#include "CurrentPointStore.h"


CurrentPointStore* CurrentPointStore::m_pInstance = NULL;
QMutex CurrentPointStore::m_Mutex;

CurrentPointStore::CurrentPointStore(QObject *parent) :
    QObject(parent)
{
}


QList<ControlPointStruct> CurrentPointStore::currentPointList() const
{
    QList<ControlPointStruct> currentPointList;
    foreach (const int &address, m_currentPointMap.keys())
    {
           ControlPointStruct controlPoint;
           int value = m_currentPointMap.value(address);
           controlPoint.setAddress(address);
           controlPoint.setValue(value);
           currentPointList.append(controlPoint);
    }
    return currentPointList;
}

bool CurrentPointStore::isHaveThisPoint(const ControlPointStruct &controlPoint)
{
    quint16 address = controlPoint.address();
    quint16 value   = controlPoint.value();
    if(m_currentPointMap.contains(address))
    {
        if(m_currentPointMap.value(address) == value)
        {
            return true;
        }
    }

    return false;
}


void CurrentPointStore::insert(const ControlPointStruct &controlPoint)
{
    m_currentPointMap.insert(controlPoint.address(),controlPoint.value());
}

void CurrentPointStore::insert(const QList<ControlPointStruct> &controlPointList)
{
    for(int i=0; i<controlPointList.size(); i++)
    {
        ControlPointStruct controlPoint = controlPointList.at(i);
        m_currentPointMap.insert(controlPoint.address(),controlPoint.value());
    }
}

void CurrentPointStore::clearnMap()
{
     m_currentPointMap.clear();
}

CurrentPointStore *CurrentPointStore::getInstance()
{
    if(!m_pInstance)
    {
        QMutexLocker lock(&m_Mutex);
        if(!m_pInstance)
        {
            CurrentPointStore *pInstance = new CurrentPointStore();
            m_pInstance = pInstance;
        }
    }
    return m_pInstance;
}
